﻿using Admin.DAL;
using Admin.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Admin.Controllers
{
    public class ProductDetailsAdminController : Controller
    {
        //
        // GET: /ProductDetails/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ProductDetails(string id)
        {
            ProductDetailsDAL pro = new ProductDetailsDAL();
            List<ProductDetailsModel> list = pro.CTSANPHAM(id);


            return View(list);
        }
        //DELETE
        public ActionResult deleteProductDetails(string id)
        {
            ProductDetailsDAL dal = new ProductDetailsDAL();
            List<ProductDetailsModel> list = dal.deletePRD(id);
            return View(list);
        }

        [HttpPost, ActionName("DeleteProductDetails")]
        [ValidateAntiForgeryToken]

        public ActionResult DeleteDetails(string id)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from CTSANPHAM where MASP='" + id + "'", conn);
            cmd.ExecuteNonQuery();
            return RedirectToAction("ProductDetails");
        }
        //INSERT
        [HttpGet]
        public ActionResult insertProductDetails(string id)
        {

            return View();
        }
         private bool isValidContentType(string contentType)
        {
            return contentType.Equals("image/png") || contentType.Equals("image/gif") ||
                contentType.Equals("image/jpg") || contentType.Equals("image/jpeg");
        }
        private bool isValidContentLength(int contentLength)
        {
            return ((contentLength / 1024) / 1024) < 1;
        }
        [HttpPost]
        public ActionResult insertProductDetails(ProductDetailsModel model,FormCollection collect, HttpPostedFileBase photo)
        {
            try
            {
                
                    var asd = new ProductDetailsModel();
                        List<ProductDetailsModel> list = new List<ProductDetailsModel>();
                    SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
                    conn.Open();

                    if (!isValidContentType(photo.ContentType))
                    {
                        ViewBag.Error = "Bạn chỉ được chọn file hình có kiểu jpg, png, gif & jpeg.";
                        return View("insertProductDetails");
                    }
                    else if (!isValidContentLength(photo.ContentLength))
                    {
                        ViewBag.Error = "File hình vượt quá dung lượng quy định (<1M)";
                        return View("insertProductDetails");
                    }
                    else
                    {
                        if (photo.ContentLength > 0)
                        {
                            var fileName = Path.GetFileName(photo.FileName);
                            var path = Path.Combine(Server.MapPath("~/Content/images/sanpham"), fileName);
                            photo.SaveAs(path);
                            ViewBag.fileName = photo.FileName;

                        }
                    }
                    ViewBag.fileName = photo.FileName;

                   
                    model.IMG = photo.FileName;
                    if (collect["tensp"] != "")
                    {
                        model.TENSP = collect["tensp"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }
                    if(collect["giacu"] !="")
                    {
                        model.GIACU = collect["giacu"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }
                    if(collect["giamoi"]!="")
                    {
                        model.GIAMOI = collect["giamoi"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }

                    if (collect["saleof"] != "")
                    {
                        model.SALEOF = collect["saleof"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }

                    if (collect["chude"] != "")
                    {
                        model.CHUDE = collect["chude"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }
                    if (collect["ngaytao"] != "")
                    {
                        model.NGAYTAO = collect["ngaytao"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }
                    if (collect["nguoitao"] != "")
                    {
                        model.NGUOITAO = collect["nguoitao"];
                    }
                    else
                    {
                        return View("insertProductDetails");  
                    }
                    
                    
                    
                    
                    
                    

                    SqlCommand command = new SqlCommand("insert into CTSANPHAM (IMG,TENSP,GIACU,GIAMOI,SALEOF,CHUDE,NGAYTAO,NGUOITAO) Values('" + model.IMG + "',N'" + model.TENSP + "',N'" + model.GIACU + "',N'" + model.GIAMOI + "',N'" + model.SALEOF + "',N'" + model.CHUDE + "',N'" + model.NGAYTAO + "',N'" + model.NGUOITAO + "')", conn);
                    SqlDataReader dr = command.ExecuteReader();

                    
                    return RedirectToAction("ProductDetails");

                }
                

            
            catch
            {
                return View();
            }

        }
        
        //EDIT
        [HttpGet]
        public ActionResult editProductDetails(string id)
        {

            ProductDetailsDAL dal = new ProductDetailsDAL();
            List<ProductDetailsModel> list = dal.editPRD(id);
            return View(list);


        
        }
                
        [HttpPost]
        public ActionResult editProductDetails(string id, ProductDetailsModel model, FormCollection collect, HttpPostedFileBase photo1)
        {

                    ProductDetailsDAL dal = new ProductDetailsDAL();
                    List<ProductDetailsModel> list = dal.editPRD(id);
                    SqlConnection conn = new SqlConnection(@"Data Source=.;Initial Catalog=LazadaDB;User ID=sa;Password=sa");
                    conn.Open();
                    
                        if (photo1 != null && !isValidContentType(photo1.ContentType))
                        {
                            ViewBag.Error = "Bạn chỉ được chọn file hình có kiểu jpg, png, gif & jpeg.";
                            return View(list);
                        }
                        else if (photo1 != null && !isValidContentLength(photo1.ContentLength))
                        {
                            ViewBag.Error = "File hình vượt quá dung lượng quy định (<1M)";
                            return View(list);
                        }
                        else
                        {
                            if (photo1 != null && photo1.ContentLength > 0)
                            {
                                var fileName = Path.GetFileName(photo1.FileName);
                                var path = Path.Combine(Server.MapPath("~/Content/images/sanpham"), fileName);
                                photo1.SaveAs(path);
                                ViewBag.fileName = photo1.FileName;

                            }
                        }


                if (ViewBag.fileName != null)
                {
                    model.IMG = ViewBag.fileName;
                }
                else
                {

                    model.IMG = collect["photo1"];
                    
                }
                if (collect["tensp"] != "")
                {
                    
                    model.TENSP = collect["tensp"];
                }
                else
                {
                    ViewBag.Errorrsp = "bạn chưa nhập ten san pham";
                    return View(list);
                }
                if (collect["giacu"] != "")
                {
                    model.GIACU = collect["giacu"];
                }
                else
                {
                    ViewBag.Error1="bạn chưa nhập giá cũ";
                    return View(list);
                }
                if (collect["giamoi"] != "")
                {
                    model.GIAMOI = collect["giamoi"];
                }
                else
                {
                    ViewBag.Error2 = "bạn chưa nhập giá mới";
                    return View(list);
                }

                if (collect["saleof"] != "")
                {
                    model.SALEOF = collect["saleof"];
                }
                else
                {
                    ViewBag.Error3 = "bạn chưa nhập Saleof";
                    return View(list);
                }

                if (collect["chude"] != "")
                {
                    model.CHUDE = collect["chude"];
                }
                else
                {
                    ViewBag.Erro4 = "bạn chưa nhập chủ đề";
                    return View(list);
                }
                if (collect["ngaytao"] != "")
                {
                    model.NGAYTAO = collect["ngaytao"];
                }
                else
                {
                    ViewBag.Error5 = "bạn chưa nhập ngày tạo";
                    return View(list);
                }
                if (collect["nguoitao"] != "")
                {
                    
                    model.NGUOITAO = collect["nguoitao"];
                }
                else
                {
                    ViewBag.Error6 = "bạn chưa nhập người tạo";
                    return View(list);
                }
                    

                SqlCommand command = new SqlCommand("Update CTSANPHAM set IMG='" + model.IMG + "',TENSP=N'" + model.TENSP + "', GIACU=N'" + model.GIACU + "', GIAMOI=N'" + model.GIAMOI + "', SALEOF=N'" + model.SALEOF + "', CHUDE=N'" + model.CHUDE + "', NGAYTAO=N'" + model.NGAYTAO + "',NGUOITAO=N'" + model.NGUOITAO + "' where MASP='" + id + "'", conn);
                SqlDataReader dr = command.ExecuteReader();
                

                return RedirectToAction("ProductDetails");

        }

	}
}